package com.xsp.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xsp.eduservice.entity.EduChapter;
import com.xsp.eduservice.entity.EduVideo;
import com.xsp.eduservice.entity.chapter.ChapterVo;
import com.xsp.eduservice.entity.chapter.VideoVo;
import com.xsp.eduservice.mapper.EduChapterMapper;
import com.xsp.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xsp.eduservice.service.EduVideoService;
import com.xsp.commonutils.servicebase.exceptionhandler.GuLiException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-02-11
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService videoService;

    //通过课程id查询查询课程大纲列表(章节和小节)
    //本方法实际与EduSubjectServiceImpl的getAllOneTwoSubject方法类似，详细注释请在其中参照
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(wrapperChapter);

        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> videoList = videoService.list(wrapperVideo);

        List<ChapterVo> finalList = new ArrayList<>();

        for (EduChapter chapter:chapterList) {
            List<VideoVo> videoVos = new ArrayList<>();
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);

            for (EduVideo video:videoList) {
                if (video.getChapterId().equals(chapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVos);
            finalList.add(chapterVo);
        }
        return finalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("chapter_id", chapterId);
        int count = videoService.count(wrapperChapter);
        if (count > 0) {
            throw new GuLiException(20001, "该章节下有小节，无法删除！");
        } else {
            int i = baseMapper.deleteById(chapterId);
            //i>0说明删除成功
            return i > 0;
        }
    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}
